﻿using Pay.Dal;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Pay.Model.Model;
using Pay.Dapper;
using Dapper;
using Dapper.Contrib.Extensions;
using Pay.Model.Interfaces;

namespace Pay.DalImplement
{
    public class RoleDalImplement : IRoleDal
    {
        public async Task<bool> DeleteMenuByRoleId(long roleId)
        {
            using (var conn = ConnectionFactory.GetReadConnection())
            {
                string sql = "delete from role_menu where roleId=@roleId";
               return await conn.ExecuteAsync(sql, new { roleId = roleId })>0?true:false;
            }
        }
        /// <summary>
        /// 更新角色
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<bool> Update(RoleInfo model)
        {
            using (var conn = ConnectionFactory.GetReadConnection())
            {

                return await conn.UpdateAsync<RoleInfo>(model);

            }

        }
        public async Task<long> Add(UserRoleInfo model)
        {
            using (var conn = ConnectionFactory.GetReadConnection())
            {

               return await conn.InsertAsync<UserRoleInfo>(model);
             
            }
        }

        public async Task<long> Add(RoleInfo model)
        {
            using (var conn = ConnectionFactory.GetReadConnection())
            {

                return await conn.InsertAsync<RoleInfo>(model);

            }
        }

        public async Task<bool> Delete(long roleId)
        {
            using (var conn = ConnectionFactory.GetWriteConnection())
            {
                var model=await GetRoleById(roleId);
                return await conn.DeleteAsync<RoleInfo>(model);
            }
        }

        /// <summary>
        /// 获取所有角色
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public async Task<RoleInfo> GetRoleById(long Id)
        {
            using (var conn = ConnectionFactory.GetReadConnection())
            {
                string sql = "select id,rolename,createtime,Description from role_info where id=@Id";

                var data = await conn.QueryFirstOrDefaultAsync<RoleInfo>(sql, new { Id = Id });

                return data;
            }
        }
        /// <summary>
        /// 获取所有角色
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public async Task<List<RoleInfo>> GetRoleByType(int type)
        {
            using (var conn = ConnectionFactory.GetReadConnection())
            {
                string sql = "select id,rolename from role_info where roletype=@type";

               var data=await conn.QueryAsync<RoleInfo>(sql, new { type = type });

                return data.ToList();
            }
        }

        public async Task<IPagedOutput<RoleInfo>> GetPage(string searchWord, DateTime? timeBegin = null, DateTime? timeEnd = null, int offset = 10, int rows = 1)
        {
            StringBuilder sb = new StringBuilder("select id,rolename,CreateTime from role_info where 1=1");
            DynamicParameters param = new DynamicParameters();
            if (!string.IsNullOrWhiteSpace(searchWord))
            {
                sb.Append(" and  rolename like '%'+@searchWord+'%'");
                param.Add("@searchWord", searchWord);
            }
            if (timeBegin.HasValue && timeEnd.HasValue)
            {
                sb.Append(" and  createtime >=@timeBegin and createTime <=@timeEnd");
                param.Add("@timeBegin", timeBegin.Value);
                param.Add("@timeEnd", timeEnd.Value);

            }

            using (var conn = ConnectionFactory.GetReadConnection())
            {
                return await conn.PagedByAsync<RoleInfo>(sb, param, offset, rows, "createTime", false);
            }
        }
        /// <summary>
        /// 更加roleid获取所有菜单
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        public async Task<List<RoleMenuInfo>> GetMenuByRoleId(long roleId)
        {
            using (var conn = ConnectionFactory.GetReadConnection())
            {
                string sql = "select Id,MenuId from role_menu where roleId=@roleId";
                var data = await conn.QueryAsync<RoleMenuInfo>(sql,new { roleId=roleId});
                return data.ToList();
            }
        }
        public async Task<List<MenuInfo>> GetAllMenu()
        {
            using (var conn = ConnectionFactory.GetReadConnection())
            {
                string sql = "select Id,MenuName,Description from menu";
                var data= await conn.QueryAsync<MenuInfo>(sql);
                return data.ToList();
            }
        }

        public async Task<long> Add(RoleMenuInfo model)
        {
            using (var conn = ConnectionFactory.GetReadConnection())
            {

                return await conn.InsertAsync<RoleMenuInfo>(model);

            }
        }

    }
}
